package jjn.round1;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Jiang Jining
 * @since 2022-11-19 0:10
 */
public class LeetCode219_ContainsDuplicateV2 {
    public boolean containsNearbyDuplicate(int[] nums, int k) {
        Map<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < nums.length; i++) {
            if (map.get(nums[i]) != null) {
                if (Math.abs(map.get(nums[i]) - i) <= k) {
                    return true;
                }
            }
            map.put(nums[i], i);
        }
        return false;
    }
}
